#include "ThreadManager.h"
#include "Gomoku.h"

Gomoku& Gomoku::m_instance = Gomoku();

Gomoku::Gomoku()
: m_arbiter(), m_currentPlayer(NULL), m_ruleThreeUse(true)
{
	m_threadManager = new ThreadManager;
	m_players[COLOR_BLACK].Color = COLOR_BLACK;
	m_players[COLOR_BLACK].Name = "Black Player";
	m_players[COLOR_WHITE].Color = COLOR_WHITE;
	m_players[COLOR_WHITE].Name = "White Player";
	m_currentPlayer = &m_players[COLOR_BLACK];
}

Gomoku::~Gomoku()
{
}

Gomoku& Gomoku::Instance()
{
	return (m_instance);
}

void Gomoku::nextPlayerTurn()
{
	if (m_arbiter.HasCurrentPlayerWon() == true)
	{
		m_players[m_currentPlayer->Color].Winner = true;
		m_arbiter.SetCurrentPlayerWin(false);
	}
	
	if (Gomoku::Instance().getPlayers()[m_players[0].Color].EnnemyPiecesCount >= 10)
	{
		m_players[m_players[0].Color].Winner = true;
		return ;
	}
	else if (Gomoku::Instance().getPlayers()[m_players[1].Color].EnnemyPiecesCount >= 10)
	{
		m_players[m_players[1].Color].Winner = true;
		return ;
	}
	if (m_currentPlayer == &m_players[0])
	{
		m_currentPlayer = &m_players[1];
	}
	else
	{
		m_currentPlayer = &m_players[0];
	}
}

void Gomoku::setRuleThreeUse(bool _value)
{
	m_ruleThreeUse = _value;
}

Board& Gomoku::getBoard()
{
	return (m_board);
}

bool Gomoku::getRuleThreeUse() const
{
	return (m_ruleThreeUse);
}

Arbiter& Gomoku::getArbiter()
{
	return (m_arbiter);
}

Gomoku::Player* Gomoku::getCurrentPlayer()
{
	return (m_currentPlayer);
}

void Gomoku::setCurrentPlayer(Gomoku::Player* _player)
{
	m_currentPlayer = _player;
}

Gomoku::Player* Gomoku::getPlayers()
{
	return (m_players);
}

ThreadManager* Gomoku::getThreadManager()
{
	return (m_threadManager);
}

void Gomoku::reset()
{
	/*BoardCase** cases = m_board.Cases;
	for (BoardCase x = 0; x < Board::WIDTH; x++)
	{
		for (BoardCase y = 0; y < Board::HEIGHT; y++)
		{
			cases[x][y] = SET_COLOR(COLOR_NONE);
		}
	}*/
	m_board.init();
	m_players[0].EnnemyPiecesCount = 0;
	m_players[1].EnnemyPiecesCount = 0;
	m_players[0].Winner = false;
	m_players[1].Winner = false;
	m_currentPlayer = &m_players[COLOR_BLACK];
}